﻿/*
* Copyright 2004-2010 mb_sx@hotmail.com
* All Rights Reserved.
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace DeskNotes
{
    public static class Logger
    {
        internal enum Output { File, Console, None };
        static Output outputType = Output.None;
        internal static string logFile = "DeskNotes_Log.txt";
        static StreamWriter sw = null;
        internal static void Open(Output output)
        {
            outputType = output;
            if (Output.File == output)
            {
                FileStream fs = File.Open(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
                            "\\" + SearchableArchive.DEFAULT_DESKNOTES_FOLDER + "\\" + logFile, FileMode.Append, FileAccess.Write);
                sw = new StreamWriter(fs);
                sw.AutoFlush = true;
                Console.SetOut(sw);
                //sw.WriteLine("LOG OPENED: " + TimeStamp);
            }
            if (Output.None != outputType)
                Console.WriteLine("LOG OPENED: " + TimeStamp);
            
        }

        internal static void Close()
        {
            if (Output.None != outputType)
                Console.WriteLine("LOG NORMALLY CLOSED: " + TimeStamp);

            if (null == sw)
                return;
            sw.Flush();
            sw.Close();
        }

        internal static void WriteLine(string s)
        {
            if (Output.None == outputType)
                return;
            string line = Logger.TimeStamp + s;
            Console.WriteLine(line);
        }
        internal static void WriteLine(string format, Object[] arg)
        {
            if (Output.None == outputType)
                return;

            string line = Logger.TimeStamp + format;
            Console.WriteLine(line, arg);
        }

        internal static void WriteLine(string format, Object arg)
        {
            if (Output.None == outputType)
                return;

            string line = Logger.TimeStamp + format;
            Console.WriteLine(line, arg);
        }

        internal static void WriteLine(string format, Object arg0,Object arg1)
        {
            if (Output.None == outputType)
                return;

            string line = Logger.TimeStamp + format;
            Console.WriteLine(line, arg0, arg1);
        }

        internal static void WriteLine(string format, Object arg0, Object arg1, Object arg2)
        {
            if (Output.None == outputType)
                return;
            
            string line = Logger.TimeStamp + format;
            Console.WriteLine(line, arg0, arg1, arg2);
        }
        static string TimeStamp
        {
            get
            {
                System.DateTime time = System.DateTime.Now;
                return "[" + time.ToShortDateString() + " " + time.ToLongTimeString() + "] ";
            }
        }
    }
}
